<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="gatewayid">
      <el-input v-model="dataForm.gatewayid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="deviceid">
      <el-input v-model="dataForm.deviceid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="当前累计流量" prop="currentcumulativeflow">
      <el-input v-model="dataForm.currentcumulativeflow" placeholder="当前累计流量"></el-input>
    </el-form-item>
    <el-form-item label="结算日累计流量" prop="accumulatedtrafficday">
      <el-input v-model="dataForm.accumulatedtrafficday" placeholder="结算日累计流量"></el-input>
    </el-form-item>
    <el-form-item label="实时时间" prop="codetime">
      <el-input v-model="dataForm.codetime" placeholder="实时时间"></el-input>
    </el-form-item>
    <el-form-item label="水表代码表示的状态" prop="codestate">
      <el-input v-model="dataForm.codestate" placeholder="水表代码表示的状态"></el-input>
    </el-form-item>
    <el-form-item label="" prop="ctime">
      <el-input v-model="dataForm.ctime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          gatewayid: '',
          deviceid: '',
          currentcumulativeflow: '',
          accumulatedtrafficday: '',
          codetime: '',
          codestate: '',
          ctime: ''
        },
        dataRule: {
          gatewayid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          deviceid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentcumulativeflow: [
            { required: true, message: '当前累计流量不能为空', trigger: 'blur' }
          ],
          accumulatedtrafficday: [
            { required: true, message: '结算日累计流量不能为空', trigger: 'blur' }
          ],
          codetime: [
            { required: true, message: '实时时间不能为空', trigger: 'blur' }
          ],
          codestate: [
            { required: true, message: '水表代码表示的状态不能为空', trigger: 'blur' }
          ],
          ctime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/xust/wtdata/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gatewayid = data.wtdata.gatewayid
                this.dataForm.deviceid = data.wtdata.deviceid
                this.dataForm.currentcumulativeflow = data.wtdata.currentcumulativeflow
                this.dataForm.accumulatedtrafficday = data.wtdata.accumulatedtrafficday
                this.dataForm.codetime = data.wtdata.codetime
                this.dataForm.codestate = data.wtdata.codestate
                this.dataForm.ctime = data.wtdata.ctime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/xust/wtdata/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'gatewayid': this.dataForm.gatewayid,
                'deviceid': this.dataForm.deviceid,
                'currentcumulativeflow': this.dataForm.currentcumulativeflow,
                'accumulatedtrafficday': this.dataForm.accumulatedtrafficday,
                'codetime': this.dataForm.codetime,
                'codestate': this.dataForm.codestate,
                'ctime': this.dataForm.ctime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
